<template>
    <div>
        <el-form ref="form" :model="form" :label-position="labelPosition" style="margin-left:5%">
            <el-form-item label="异议说明 ：">
                <el-input :disabled="true" type="textarea" v-model="form.declareNote"
                          :autosize="{ minRows: 4, maxRows: 10}"></el-input>
            </el-form-item>
        </el-form>

        <el-table
                :data="fileList"
                border
                style="width: 95%; margin-left: 5%"
                @cell-click="downloadFile">
            <el-table-column
                    prop="name"
                    label="文件名">
            </el-table-column>
            <!--<el-table-column
                    prop="updateDate"
                    label="操作"
                    width="105px">
                <template slot-scope="scope">
                    <el-button
                            v-if="scope.row.type == 1"
                            size="mini"
                            type="text"
                            @click="deleteFile(scope.row)">删除
                    </el-button>
                </template>
            </el-table-column>-->
        </el-table>

        <el-dialog
                :title="fileTitle"
                :visible.sync="fileVisible"
                width="95%"
                center>
            <pdf-shower v-if="fileType" :url="fileUrl" style="width: 100%"></pdf-shower>
            <img v-if="!fileType" :src="fileUrl" style="width: 100%"/>
        </el-dialog>

        <el-dialog
                title="上传资料详细说明"
                :visible.sync="showExplain"
                width="95%"
                center>
            <p>
                <span class="detail">
                   1、资料数量最多只能30份, 多余30份请于线下邮寄。<br/><br/>
                </span>
                <span class="detail">
                    2、为方便管理人审核您的债权，建议您用铅笔对您的债权材料编号，再按顺序拍照上传。<br/><br/>
                </span>
            </p>
            <span slot="footer" class="dialog-footer">
                <el-button type="primary" style="width: 80%" @click="showExplain = false">确  定</el-button>
            </span>
        </el-dialog>
        <div style="text-align: center;width: 100%" v-if="showDissentButton">
            <el-button @click.native="submit" type="primary" style="width: 80%; margin-top: 5%">修改异议</el-button>
        </div>
    </div>
</template>

<script>
    import UploadFile from 'components/upload/upload-button-text.vue'
    import util from 'assets/js/util.js'
    import conf from 'assets/js/conf.js'
    import PdfShower from 'components/pdf/pdf-shower.vue'

    export default {
        data() {
            return {
                labelPosition: 'left',
                isShow: false,
                fileList: [],
                fileTitle: '',
                declareNote: '',
                fileUrl: '',
                fileType: false,
                fileVisible: false,
                total: 0,
                showExplain: false,
                caseId: 0,
                creditorId: 0,
            };
        },
        props: [
            'form',
            'showDissentButton',
        ],
        mounted: function () {
            this.id = util.getUrlParam(document.location.href, 'id');
            const _self = this;

            util.ajax({
                url: 'file/getFileList',
                params: {eids: _self.form.declareFileids},
                success: function (data) {
                    data.data.forEach(_file => _self.fileList.push({
                        name: _file.name,
                        id: _file.id
                    }))
                },
            });
        },
        methods: {
            uploadFile: function (file) {
                if (this.fileList.length > 30) {
                    alert('资料数量太多了, 请于线下邮寄');
                    return;
                }
                this.fileList.push({
                    id: file.id,
                    type: 1,
                    count: this.fileList.length + 1,
                    name: '异议说明 ' + (this.fileList.length + 1),
                    updateDate: util.formatDate(file.updateDate)
                });
                if (this.form.declareFileids === '')
                    this.form.declareFileids = file.id;
                else
                    this.form.declareFileids = this.form.declareFileids + ',' + file.id;
            },
            deleteFile: function (row) {
                this.fileList.splice(row.count - 1, 1);
                this.form.declareFileids = '';
                if (this.fileList.length > 1) {
                    this.fileList.forEach(_file => this.form.declareFileids += _file.id + ',');
                    this.form.declareFileids = this.form.declareFileids.substring(0, this.form.declareFileids.length - 1);
                }
                let count = 0;
                this.fileList.forEach(_file => {
                    _file.name = '异议说明 ' + (++count);
                    _file.count = count;
                })
            },
            downloadFile: function (row, column, cell, event) {
                if (column.label === '操作')
                    return;
                this.fileUrl = conf.baseUrl + '/file/get/' + row.id + '?type=1';
                this.fileTitle = row.name;
                if (row.type === 0) {
                    this.fileType = true;
                } else {
                    this.fileType = false;
                }
                this.fileVisible = true;
            },
            submit: function () {
                window.location.href = '../credit/dissent.html?' + 'creditId=' + util.getUrlParam(document.location.href, 'id') + "&declareNote=" + encodeURI(this.form.declareNote) + "&declareFileids=" + this.form.declareFileids;
                /*const _self = this;
                util.ajax({
                    url:`/credit/objection`,
                    params: {
                        creditId: util.getUrlParam(document.location.href, 'id'),
                        declareNote: this.form.declareNote,
                        declareFileids: this.form.declareFileids
                    },
                    success: function (data) {
                        _self.$message({
                            message: '提交成功!',
                            center: true
                        });
                        window.location.reload();
                    },
                });*/
            }
        },
        components: {
            UploadFile,
            PdfShower
        }
    }
</script>